#ifndef _NETCAP_H
#define _NETCAP_H
#include <string>
#include <pcap.h>
#include <map>
#include <vector>
#include <iostream>
#ifdef WIN32
#include <stdint.h>
#else
#include <arpa/inet.h>
#include <pthread.h>
#endif

#include "event.h"
using namespace std;
#define PACKETTHRESHOLD 2000


typedef struct  linkstat{
 	unsigned int  source_ip;
	unsigned int	dest_ip;
       unsigned short  source_port;  
	unsigned short   dest_port;
	unsigned int   appertimes;
	unsigned int     protol ;
}linkstat ;

typedef struct _netcardinfo{
	char name[256];
	char desc[256];
	unsigned char mac[6];
	unsigned long ip;
	unsigned long mask;
}netcardinfo;

typedef struct _recvframedescri
{
    vector<unsigned char> data;
    struct pcap_pkthdr pkth;
    
} recvframedescri ;

typedef struct  macinfo
{
    unsigned int  times;
    struct in_addr ip ;
} macinfo ;


typedef struct _ipinfo
{
    char ip[20];
    unsigned int  times;
} ipinfo ;


class  NetCap
{
public:
	NetCap(string filtRule);
	~NetCap( );
	static void * WatchingDeal(void* param);
    static void * ping(void* param);
    static void * ipdetect(void* param);
	void CreateCap(bool netMode,string filtRule);
	void SetFilter(string filtRule);
	void CapDeal(unsigned int  *user, const  struct pcap_pkthdr *pkthdr, const u_char *packet);
	void  GetFlowStatist(string s);	
	void GetNetSpeed();
	void NetSpeedMonitor(void *arg);
	unsigned int GetByteSize();
	void GetByteSize(unsigned int totals);
	int getMac(const char *iface, int *ifindex, unsigned int *addr, unsigned char *mac);
	void getNetCardList(std::map<std::string,netcardinfo>& v );
	void findmac(char *dev , unsigned char *mac );
    unsigned int getnetflag();
    string getFilterRule();
    uint64_t getNetCradFlow();
    uint64_t getLocalNetUpFlow();
    uint64_t getLocalNetDownFlow();
    string   getmacs(std::vector<pair<uint64_t,macinfo> > &macs);
    int  getlinkstat(std::vector<linkstat> &linklists);
    void arpx( unsigned long host, unsigned long mask );
    void getdetect(std::map<std::string ,uint64_t>&ips);
    //std::vector<recvframedescri> datafifo;
    
    std::map<std::string, netcardinfo> m_NetCardList;
    
    std::map<uint64_t,macinfo> maclist; 
    std::map<std::string ,uint64_t > ipdetectinfo;
	
    std::vector<linkstat> linklist;
    in_addr ip;
    unsigned long  mask;
    #ifdef WIN32
            int threadid; 
            int threadid1;
	    #else		
          pthread_t  threadid;
          pthread_t  threadid1;
    #endif
    unsigned int flag;
    char * devName ;
    bool ipdetectcontrl;
    event_handle ipevent;
    event_handle macevent;
    event_handle event;	
private:
    unsigned char uploadhost[6];
	pcap_t * _pcap; 
	char errBuf[PCAP_ERRBUF_SIZE];
		
	string filterRule;
	struct bpf_program filter;
	long unsigned int totals;
	long unsigned int uptotals;
	long unsigned int downtotals;
	long unsigned int filtertotals;
	unsigned int  curpacketnum;
       unsigned int  packet_per_second;
    
	struct pcap_pkthdr packet_descri;

     
};


#endif
